const Router = require('koa-router')
const router = new Router()
const mongoose = require('mongoose')

//后台写博客接口
router.post('/publish',async(ctx)=>{
    try{
     const publish = mongoose.model("blogs")
     const info = new publish(ctx.request.body)
     await info.save()
     ctx.body = {code:200,message:"存入数据库成功"}    
    }
    catch(error){
        console.log(error);    
    }
 })
 //获取所有标签
 router.get('/gettags',async(ctx)=>{
    try{
        const tags = mongoose.model("tags")
        const result = await tags.find()
        console.log(result);
        
        ctx.body = {code:200,message:result}    
       }
       catch(error){
           console.log(error);
       }
})
 //获取所有博客
 router.get('/getallblogs',async(ctx)=>{
     try{
         const blogs = mongoose.model("blogs")
         const result = await blogs.find()
         ctx.body = {code:200,message:result}    
        }
        catch(error){
            console.log(error);
        }
 })
 
 //保存草稿到数据库
 router.post('/savetodraft',async(ctx)=>{
     try{
         const drafts = mongoose.model("drafts")
         const saveDrafts = new drafts(ctx.request.body)
         const result = await saveDrafts.save()
         ctx.body = {code:200,message:result}    
        }
        catch(error){
            console.log(error);
        }
 })
 //获取草稿
 router.get('/getdraft',async(ctx)=>{
     try{
         const drafts = mongoose.model("drafts")
         const result = await drafts.find()
         ctx.body = {code:200,message:result}    
        }
        catch(error){
            console.log(error);
        }
 })
 //获取一个草稿到编辑器
 router.post('/getonedraft',async(ctx)=>{
     try{
         const drafts = mongoose.model("drafts")
         const result = await drafts.findOne(ctx.request.body)
         ctx.body = {code:200,message:result}    
        }
        catch(error){
            console.log(error);
        }
 })
 //获取所有评论
 router.get('/getallcomments',async(ctx)=>{
     try{
         const usercomments = mongoose.model("usercomments")
         const result = await usercomments.find()
         ctx.body = {code:200,message:result}    
        }
        catch(error){
            console.log(error);
        }
 })
 //删除评论
 router.post('/deletecomments',async(ctx)=>{
     try{
         const usercomments = mongoose.model("usercomments")
         const content = ctx.request.body.content
         const result = await usercomments.remove({content:content})
         ctx.body = {code:200,message:"删除成功"+result}    
        }
        catch(error){
            console.log(error);
        }
 })
 //删除博客
 router.post('/deleteblog',async(ctx)=>{
     try{
      const test = mongoose.model("blogs")
      const title = ctx.request.body.title
      const result = await test.remove({title:title})
      ctx.body = {code:200,message:"删除成功"+result}    
     }
     catch(error){
         console.log(error);
     }
  })
  //获取一个要修改的博客
  router.post('/getonemodifyblog',async(ctx)=>{
     try{
         const blogs = mongoose.model("blogs")
         const result = await blogs.findOne(ctx.request.body)
         ctx.body = {code:200,message:result}    
        }
        catch(error){
            console.log(error); 
        }
 })
 
 //添加标签
  router.post('/addtags',async(ctx)=>{
     try{
         const tags = mongoose.model("tags")
         const addTags = new tags(ctx.request.body)
         const result = await addTags.save()
         ctx.body = {code:200,message:"添加成功"}    
        }
        catch(error){
            console.log(error); 
        }
 })
 //删除标签
 router.post('/deltags',async(ctx)=>{
     try{
         const tags = mongoose.model("tags")
         const tagName = ctx.request.body.tagName
         //注意要是ctx.request.body.tagName，不是.value
         const result = await tags.deleteOne({tagName:tagName})
         console.log(tagName);
         
         ctx.body = {code:200,message:"删除成功"+result}    
        }
        catch(error){
            console.log(error); 
        }
 })
module.exports = router
